package com.sdp.wx.db.wx_wz_now_num.service.impl;

import com.sdp.core.bizc.db.IViewQuery;
import com.sdp.wx.db.wx_wz_now_num.entity.WxWzNowNum;
import com.sdp.wx.db.wx_wz_now_num.mapper.WxWzNowNumMapper;
import com.sdp.wx.db.wx_wz_now_num.service.IWxWzNowNumService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-01-29
 */
@Service
public class WxWzNowNumServiceImpl extends ServiceImpl<WxWzNowNumMapper, WxWzNowNum> implements IWxWzNowNumService,IViewQuery {
	
	@Autowired
	private WxWzNowNumMapper wxWzNowNumMapper;

	@Override
	public void wzNowNumAdd(List<String> wzCodeList, Map<String, BigDecimal> wzInputNumMap,String warehouseCode) {
		if(wzCodeList==null || wzCodeList.size()==0) {
			return;
		}
		WxWzNowNum wzNowNum = null;
		List<WxWzNowNum> queryWzNowNumList = queryWzNowNumByWzCodes(wzCodeList,warehouseCode);
		if(queryWzNowNumList==null || queryWzNowNumList.size()==0) {
			List<WxWzNowNum> wzNowNumList = new ArrayList<>();
			for(Map.Entry<String, BigDecimal> entry : wzInputNumMap.entrySet()){
				wzNowNum = new WxWzNowNum();
				wzNowNum.setWzNo(entry.getKey());
				wzNowNum.setNowNum(entry.getValue());
				wzNowNum.setAlertNumber(BigDecimal.ZERO);
				wzNowNum.setCreateTime(LocalDateTime.now());
				wzNowNum.setWarehouseCode(warehouseCode);
				wzNowNumList.add(wzNowNum);
				
			}
			saveBatch(wzNowNumList);
		}else {
			List<WxWzNowNum> insertNowNumList = new ArrayList<>();
			List<WxWzNowNum> updateNowNumList = new ArrayList<>();
			for(WxWzNowNum wz : queryWzNowNumList) {
				if(wzInputNumMap.containsKey(wz.getWzNo())) {
					wz.setNowNum(wz.getNowNum().add(wzInputNumMap.get(wz.getWzNo())));
					updateNowNumList.add(wz);
					wzInputNumMap.remove(wz.getWzNo());
				}
			}
			
			if(wzInputNumMap.size()>0) {
				for (Map.Entry<String,BigDecimal> entry : wzInputNumMap.entrySet()) {
					wzNowNum = new WxWzNowNum();
					wzNowNum.setWzNo(entry.getKey());
					wzNowNum.setNowNum(entry.getValue());
					wzNowNum.setAlertNumber(BigDecimal.ZERO);
					wzNowNum.setCreateTime(LocalDateTime.now());
					wzNowNum.setWarehouseCode(warehouseCode);
					insertNowNumList.add(wzNowNum);
				}
				
				saveBatch(insertNowNumList);
			}
			
			if(updateNowNumList.size()>0) {
				updateBatchById(updateNowNumList);
			}
		}
		
	}

	@Override
	public void wzNowNumSubtract(List<String> wzCodeList, Map<String, BigDecimal> wzOutputNumMap,String warehouseCode) {
		if(wzCodeList==null || wzCodeList.size()==0) {
			return;
		}
		
		List<WxWzNowNum> queryWzNowNumList = queryWzNowNumByWzCodes(wzCodeList,warehouseCode);
		if(queryWzNowNumList!=null && queryWzNowNumList.size()>0) {
			List<WxWzNowNum> updateNowNumList = new ArrayList<>();
			for(WxWzNowNum wz : queryWzNowNumList) {
				if(wzOutputNumMap.containsKey(wz.getWzNo())) {
					if(wz.getNowNum().compareTo(wzOutputNumMap.get(wz.getWzNo()))>0) {
						wz.setNowNum(wz.getNowNum().subtract(wzOutputNumMap.get(wz.getWzNo())));
					}else {
						wz.setNowNum(BigDecimal.ZERO);
					}
					
					updateNowNumList.add(wz);
				}
			}
			if(updateNowNumList.size()>0) {
				updateBatchById(updateNowNumList);
			}
		}
		
	}

	@Override
	public List<WxWzNowNum> queryWzNowNumByWzCodes(List<String> wzNos,String warehouseCode) {
		LambdaQueryWrapper<WxWzNowNum> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(WxWzNowNum::getWzNo, wzNos);
		queryWrapper.eq(WxWzNowNum::getWarehouseCode, warehouseCode);
		return list(queryWrapper);
	}
	
	
	@Override
	public List<WxWzNowNum> queryWzNowNumByWarehouseCode(String warehouseCode) {
		LambdaQueryWrapper<WxWzNowNum> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(WxWzNowNum::getWarehouseCode, warehouseCode);
		return list(queryWrapper);
	}

	@Override
	public <T> List<T> viewList(QueryWrapper<T> queryWrapper) {
		return wxWzNowNumMapper.viewList(queryWrapper);
	}

	@Override
	public <T> int viewCount(QueryWrapper<T> queryWrapper) {
		return wxWzNowNumMapper.viewCount(queryWrapper);
	}

	@Override
	public <T> IPage<T> viewPage(IPage<T> page, QueryWrapper<T> queryWrapper) {
		return wxWzNowNumMapper.viewPage(page, queryWrapper);
	}
	
	
	public <T> List<T> queryAllWzNowNum(QueryWrapper<T> queryWrapper){
		return wxWzNowNumMapper.queryAllWzNowNum(queryWrapper);
	}

	@Override
	public int wzAlertNumber() {
		LambdaQueryWrapper<WxWzNowNum> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.apply("now_num<=alert_number");
		return count(queryWrapper);
	}

}
